#include "userinfowidget.h"
#include <QGridLayout>
#include <QPushButton>
#include <QApplication>
#include <QMessageBox>
#include "debug.h"
#include "model/datacenter.h"
#include "mainWidget.h"
#include "toast.h"

userInfoWidget::userInfoWidget(QWidget* parent,const UserInfo* userInfo)
:QDialog(parent)
{
    //1.设置窗口属性
    this->setFixedSize(500/SCALE,250/SCALE);
    this->setWindowTitle("用户详情");
    this->setWindowIcon(QIcon(":/myResource/Image/WeChat.svg"));
    //关闭窗口时，自动销毁对象
    this->setAttribute(Qt::WA_DeleteOnClose);
    //把窗口移动到当前鼠标位置
    this->move(QCursor::pos());

    //2.设置布局管理器
    QGridLayout* layout = new QGridLayout;
    layout->setAlignment(Qt::AlignTop);
    this->setLayout(layout);

    //3.设置窗口中的控件

    //设置头像
    avatarBtn = new QPushButton;
    avatarBtn->setFixedSize(100/SCALE,100/SCALE);
    avatarBtn->setIcon(userInfo->avatar);
    avatarBtn->setIconSize(QSize(100/SCALE,100/SCALE));
    layout->addWidget(avatarBtn,0,0,3,1,Qt::AlignTop);

    //设置其他属性
    QString labelStyle = "QLabel{font-size:14px;font-weight:800;}";
    // QString btnStyle="QPushButton:pressed { background-color: rgb(0,0,0); }";
    QString btnStyle = "QPushButton {border:1px solid black;border-radius:5px; }";
    btnStyle += "QPushButton:pressed { background-color: rgb(210, 210, 210); }";
    QString editStyle="QLineEidt{}";
    int height = 40/SCALE;

    //用户ID
    idTag = new QLabel;
    idTag->setFixedSize(50,height);
    idTag->setText("用户ID:");
    idTag->setStyleSheet(labelStyle);
    idTag->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
    idLabel = new QLabel;
    idLabel->setFixedHeight(height);
    idLabel->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    idLabel->setText(userInfo->userId);

    //名字
    nameTag = new QLabel;
    nameTag->setFixedSize(50,height);
    nameTag->setText("姓名:");
    nameTag->setStyleSheet(labelStyle);
    nameTag->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
    nameLabel = new QLabel;
    nameLabel->setFixedHeight(height);
    nameLabel->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    nameLabel->setText(userInfo->nickname);


    //签名
    descTag = new QLabel;
    descTag->setFixedSize(50,height);
    descTag->setText("签名:");
    descTag->setStyleSheet(labelStyle);
    descTag->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
    descLabel = new QLabel;
    descLabel->setFixedHeight(height);
    descLabel->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    descLabel->setText(userInfo->description);

    //添加三个按钮
    sendMessageBtn = new QPushButton;

    sendMessageBtn->setFixedSize(130/SCALE,40);
    sendMessageBtn->setText("发送消息");
    sendMessageBtn->setStyle(QApplication::style());
    sendMessageBtn->setStyleSheet(btnStyle);
    qDebug() << sendMessageBtn->styleSheet();
    applyBtn = new QPushButton;
    applyBtn->setFixedSize(130/SCALE,40);
    applyBtn->setText("申请好友");
    applyBtn->setStyleSheet(btnStyle);
    deleteFriendBtn = new QPushButton;
    deleteFriendBtn->setFixedSize(130/SCALE,40);
    deleteFriendBtn->setText("删除好友");
    deleteFriendBtn->setStyleSheet(btnStyle);

    //4.添加到布局管理器中
    layout->addWidget(idTag,0,1);
    layout->addWidget(idLabel,0,2);

    layout->addWidget(nameTag,1,1);
    layout->addWidget(nameLabel,1,2);

    layout->addWidget(descTag,2,1);
    layout->addWidget(descLabel,2,2);

    layout->addWidget(sendMessageBtn,3,0);
    layout->addWidget(applyBtn,3,1);
    layout->addWidget(deleteFriendBtn,3,2);

    //5.设置禁用按钮情况
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    UserInfo* myFriend = dataCenter->findFriendByUId(userInfo->userId);
    if(myFriend)
        applyBtn->setEnabled(false);
    else
    {
        sendMessageBtn->setEnabled(false);
        deleteFriendBtn->setEnabled(false);
    }

    //6.初始化信号和槽
    initSignalSlot();



}

void userInfoWidget::initSignalSlot()
{
    connect(sendMessageBtn,&QPushButton::clicked,this,[=]()
    {
        MainWidget* mainWidget = MainWidget::getInstance();
        mainWidget->switchSession(idLabel->text());
        this->close();
    });
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    connect(deleteFriendBtn,&QPushButton::clicked,this,[=]()
    {
        emit dataCenter->deleteFriend(idLabel->text());
        this->close();
    });
    connect(applyBtn,&QPushButton::clicked,this,&userInfoWidget::clickApplyBtn);
}

void userInfoWidget::clickApplyBtn()
{
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    dataCenter->sendApplyAsync(idLabel->text());
    this->close();
}
